<& /Minimal/Elements/Header, Title => loc('Preferences') &>
<& /Elements/ListActions, actions => \@results &>
<form method="post">

% unless (RT->Config->Get('WebExternalAuth') and !RT->Config->Get('WebFallbackToInternalAuth')) {
<&| /Widgets/TitleBox, title => loc('Change password')  &>
<&|/l&>New password</&>: <input type="password" name="NewPass1" size="16" autocomplete="off" />
<&|/l&>Confirm</&>: <input type="password" name="NewPass2" size="16" autocomplete="off" />
<& /Elements/Submit, Label => loc('Save Changes') &>
</&>
<br />
% }
%#	  </form>

<table>
<tr>
<td>
% require RT::Crypt::GnuPG;
% my %res = RT::Crypt::GnuPG::GetKeyInfo( $UserObj->EmailAddress(), 'Public' );
% if($res{'exit_code'} || !keys %{ $res{'info'} } ){
<&|/Widgets/TitleBox, title => loc( 'Upload PGP Public Key' ) &>
<textarea cols="80" rows="25" name="UploadKey" wrap="soft"></textarea>
<& /Elements/Submit, Name => 'Upload', Label => loc('Upload Key') &>
</&>
% } else {
<&|/Widgets/TitleBox, title => loc( 'Purge Key' ) &>
<&|/l&><b>Key</b></&>: <& /Elements/GnuPG/SelectKeyForEncryption, EmailAddress => $UserObj->EmailAddress &>
<& /Elements/Submit, Name => 'PurgeKey', Label => loc('Purge Key') &>
</&>
% }

</td>
</tr>
</table>
</form>

<%INIT>
my @results;
my $UserObj = $session{'CurrentUser'}->UserObj;

if($PurgeKey){
    require RT::IR::Federation;
    RT::IR::Federation::PurgeKey($PreferredKey);
    push(@results, 'Key '.$PreferredKey.' Purged');
}

if($UploadKey){
    unless($UploadKey =~ /^\-\-\-\-\-BEGIN PGP PUBLIC KEY BLOCK\-\-\-\-\-/ && $UploadKey =~ /\-\-\-\-\-END PGP PUBLIC KEY BLOCK\-\-\-\-\-/){
        push(@results,'Public Keys Only!');
    } else {
		my %opt = RT->Config->Get('GnuPGOptions');
        my $g = GnuPG::Interface->new();

		$g->options->hash_init(
        	RT::Crypt::GnuPG::_PrepareGnuPGOptions( %opt ),
        	armor => 1,
        	meta_interactive => 0,
    	);
		
        my ($input,$output) = (IO::Handle->new(), IO::Handle->new());
        my $handles = GnuPG::Handles->new(stdin => $input, stdout => $output);

        my $pid = $g->import_keys(handles => $handles);

        print $input $UploadKey;
        close($input);
        push(@results, loc('Key successfully Uploaded')."\n$UploadKey");
    }
}


if($NewPass1) {
    if ($NewPass1 ne $NewPass2) {
	push (@results, "Passwords did not match.");
    }	
    else {
	my ($val, $msg)=$session{'CurrentUser'}->UserObj->SetPassword($NewPass1);
	push (@results, "Password: ".$msg);
    }	
}
if($Signature) {
    $Signature =~ s/(\r\n|\r)/\n/g;
    if ($Signature ne $session{'CurrentUser'}->UserObj->Signature) {
	my ($val, $msg)=$session{'CurrentUser'}->UserObj->SetSignature($Signature);
	push (@results, "Signature: ".$msg);
    }
}

#A hack to make sure that session gets rewritten.

$session{'i'}++;
</%INIT>

<%ARGS>
$Update             => 0,
$GenKey             => 0,
$UploadKey          => undef,
$Key                => undef,
$Comment            => undef,
$PreferredKey       => undef,
$PurgeKey           => undef,

$Signature => undef
$NewPass1 => undef
$NewPass2 => undef
</%ARGS>
